package com.kgcx.coupon.service;

import com.kgcx.coupon.domain.CouponDetail;
import com.kgcx.coupon.domain.PullConfig;
import com.kgcx.coupon.domain.PullRule;
import com.kgcx.coupon.dto.ConfigDto;
import com.kgcx.coupon.dto.CouponDetailDto;
import com.kgcx.coupon.dto.PullRuleDto;
import com.kgcx.coupon.mapper.CouponDetailMapper;
import com.kgcx.coupon.mapper.PullConfigMapper;
import com.kgcx.coupon.mapper.PullRuleMapper;
import java.util.Date;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 何凯
 * @version 0.1
 * @description 拉新配置
 * @time 2017/12/15 18:17
 * @since 0.1
 */
@Service
public class PullConfigService {
    @Autowired
    private PullConfigMapper pullConfigMapper;
    @Autowired
    private CouponDetailService couponDetailService;
    @Autowired
    private PullRuleService pullRuleService;
    @Resource
    private PullRuleMapper pullRuleMapper;
    @Resource
    private CouponDetailMapper couponDetailMapper;

    /**
     * 新增拉新配置
     * @param configDto
     */
	@Transactional(rollbackFor = Exception.class)
	public void saveConfig(ConfigDto configDto) {
		PullConfig pullConfigObj = selectPullConfig();
		if (null == pullConfigObj) {
			PullConfig pullConfig = new PullConfig();
			pullConfig.setCouponId(configDto.getCouponId());
			pullConfig.setCouponQty(configDto.getCouponQty());
			pullConfig.setStatus(configDto.getStatus());
			pullConfig.setStrategyStatus(configDto.getStrategyStatus());
			pullConfig.setRemark(configDto.getRemark());
			pullConfig.setCreateId(configDto.getUpdateId());
			pullConfig.setCreateTime(new Date());
			pullConfig.setUpdateId(configDto.getUpdateId());
			pullConfig.setUpdateTime(new Date());
			pullConfig.setUpdateBy(configDto.getUpdateBy());
			pullConfigMapper.insert(pullConfig);
			for (PullRuleDto pullRuleDto : configDto.getPullRuleList()) {
				PullRule pullRule = new PullRule();
				pullRule.setPullConfigId(pullConfig.getKid());
				pullRule.setCouponId(pullRuleDto.getCouponId());
				pullRule.setPullTotal(pullRuleDto.getPullTotal());
				pullRule.setCouponQty(pullRuleDto.getCouponQty());
				pullRule.setOrder(1);
				pullRuleService.insertSelective(pullRule);
			}
			for (CouponDetailDto couponDetailDto : configDto.getCouponDetailList()) {
				CouponDetail couponDetail = new CouponDetail();
				couponDetail.setPlanId(0);
				couponDetail.setCouponId(couponDetailDto.getCouponId());
				couponDetail.setCouponName(couponDetailDto.getCouponName());
				couponDetail.setType(2);//类型 1营销策略 2新人策略
				couponDetail.setCouponQty(couponDetailDto.getCouponQty());
				couponDetailService.saveCouponDetail(couponDetail);
			}
		}
	}

	/**
	 * 修改拉新配置
	 * @param configDto
	 */
	@Transactional(rollbackFor = Exception.class)
	public void modifyConfig(ConfigDto configDto) {
		PullConfig pullConfig = new PullConfig();
		pullConfig.setCouponId(configDto.getCouponId());
		pullConfig.setCouponQty(configDto.getCouponQty());
		pullConfig.setStatus(configDto.getStatus());
		pullConfig.setStrategyStatus(configDto.getStrategyStatus());
		pullConfig.setRemark(configDto.getRemark());
		pullConfig.setCreateId(configDto.getUpdateId());
		pullConfig.setCreateTime(new Date());
		pullConfig.setUpdateId(configDto.getUpdateId());
		pullConfig.setUpdateTime(new Date());
		pullConfig.setKid(configDto.getConfigKid());
		pullConfig.setUpdateBy(configDto.getUpdateBy());
		pullConfigMapper.updateByPrimaryKeySelective(pullConfig);
		if (null != configDto.getPullRuleList() && configDto.getPullRuleList().size() > 0) {
			pullRuleMapper.deleteByConfigId(configDto.getConfigKid());
			for (PullRuleDto pullRuleDto : configDto.getPullRuleList()) {
				PullRule pullRule = new PullRule();
				pullRule.setPullConfigId(pullConfig.getKid());
				pullRule.setCouponId(pullRuleDto.getCouponId());
				pullRule.setPullTotal(pullRuleDto.getPullTotal());
				pullRule.setCouponQty(pullRuleDto.getCouponQty());
				pullRule.setOrder(1);
				pullRuleService.insertSelective(pullRule);
			}
		}
		if (null != configDto.getCouponDetailList() && configDto.getCouponDetailList().size() > 0) {
			couponDetailMapper.deleteByType(2);//类型 1营销策略 2新人策略
			for (CouponDetailDto couponDetailDto : configDto.getCouponDetailList()) {
				CouponDetail couponDetail = new CouponDetail();
				couponDetail.setPlanId(0);
				couponDetail.setCouponId(couponDetailDto.getCouponId());
				couponDetail.setCouponName(couponDetailDto.getCouponName());
				couponDetail.setType(2);//类型 1营销策略 2新人策略
				couponDetail.setCouponQty(couponDetailDto.getCouponQty());
				couponDetailService.saveCouponDetail(couponDetail);
			}
		}
	}

    /**
     * 获取拉新配置 status为1正常
     * @return
     */
    public PullConfig selectPullConfig() {
    		return pullConfigMapper.selectPullConfig();
    }
    
    /**
     * 获取拉新配置 status不做条件
     * @return
     */
	public PullConfig selectPullConfigNoStatus() {
		return pullConfigMapper.selectPullConfigNoStatus();
	}
}
